package com.atm.atmsys.controller.YinLianController;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.zaxxer.hikari.util.ClockSource;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.nio.file.Files;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/YinLianPay")
@Slf4j
@AllArgsConstructor
public class PayController {

    private final Config config;
    @GetMapping("/shoukuan")
    @ApiOperation("收款二维码生成")
    public ResponseEntity<Map<String, String>> pay(@RequestParam String total) throws Exception {
        Random random = new Random();
        long min = 1000000000L;
        long max = 9999999999L;
        long randomNum = min + (long)(random.nextDouble() * (max - min + 1));

        Factory.setOptions(config);
        AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate("收款", String.valueOf(randomNum), total);

        // 解析响应
        String httpBody = response.getHttpBody();
        JSONObject jsonObject = JSONObject.parseObject(httpBody);
        String qrUrl = jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();

        // 生成二维码并保存为临时文件
        File tempFile = File.createTempFile("qrcode", ".png");
        QrCodeUtil.generate(qrUrl, 300, 300, tempFile);

        // 将二维码文件转换为Base64
        byte[] fileContent = Files.readAllBytes(tempFile.toPath());
        String qrCodeBase64 = Base64.getEncoder().encodeToString(fileContent);

        // 删除临时文件
        tempFile.delete();

        // 构建返回的JSON响应
        Map<String, String> responseMap = new HashMap<>();
        responseMap.put("qr_code_base64", qrCodeBase64);
        responseMap.put("qr_url", qrUrl);

        return ResponseEntity.ok(responseMap);
    }

    @GetMapping("/fukuan")
    @ApiOperation("付款二维码生成")
    public ResponseEntity<Map<String, String>> fukuan() throws Exception {
        Random random = new Random();
        long min = 1000000000L;
        long max = 9999999999L;
        long randomNum = min + (long)(random.nextDouble() * (max - min + 1));

        Factory.setOptions(config);
        AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate("付款", String.valueOf(randomNum),"1");

        // 解析响应
        String httpBody = response.getHttpBody();
        JSONObject jsonObject = JSONObject.parseObject(httpBody);
        String qrUrl = jsonObject.getJSONObject("alipay_trade_precreate_response").get("qr_code").toString();

        // 生成二维码并保存为临时文件
        File tempFile = File.createTempFile("qrcode", ".png");
        QrCodeUtil.generate(qrUrl, 300, 300, tempFile);

        // 将二维码文件转换为Base64
        byte[] fileContent = Files.readAllBytes(tempFile.toPath());
        String qrCodeBase64 = Base64.getEncoder().encodeToString(fileContent);

        // 删除临时文件
        tempFile.delete();

        // 构建返回的JSON响应
        Map<String, String> responseMap = new HashMap<>();
        responseMap.put("qr_code_base64", qrCodeBase64);
        responseMap.put("qr_url", qrUrl);

        return ResponseEntity.ok(responseMap);
    }

    @PostMapping("/notify")
    public String notify(HttpServletRequest request) {
        log.info("成功通知");
        String no=request.getParameter("out_trade_no");
        log.info("收款号："+no);
        return "success";
    }
}
